package com.jn.erp.wechat.staff.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.annotation.Log;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.common.enums.BusinessType;
import com.jn.erp.common.exception.ServiceException;
import com.jn.erp.common.utils.StringUtils;
import com.jn.erp.security.entity.SysUser;
import com.jn.erp.security.sysservice.SysUserRoleService;
import com.jn.erp.security.sysservice.SysUserService;
import com.jn.erp.web.api.institution.entity.vo.EmployeeInfoDto;
import com.jn.erp.web.api.institution.entity.vo.EmployeeStudentDto;
import com.jn.erp.web.api.institution.entity.vo.EmployeeVo;
import com.jn.erp.web.api.institution.service.EmployeeService;
import com.jn.erp.web.api.student.service.StudentService;
import com.jn.erp.web.api.sys.entity.SysOrgUser;
import com.jn.erp.web.api.sys.service.SysDictDataService;
import com.jn.erp.web.api.sys.service.SysOrgUserService;
import com.jn.erp.wechat.common.api.controller.base.BaseWxAuthController;
import com.jn.erp.wechat.common.core.WxTableDataInfo;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 *  老师/员工管理 相关接口管理
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/10 17:48
 */

@RestController
@RequestMapping("/wx/Staff")
public class MpStaffController extends BaseWxAuthController {


    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysOrgUserService sysOrgUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysDictDataService sysDictDataService;


    @Autowired
    private StudentService studentService;

    /* --------------------------  以下为员工管理接口  ------------------------------*/

    /**
     * 员工管理-全部列表    /institution/staff/employees
     */
    @PreAuthorize("@ss.hasPermi('institution:staff:query')")
    @GetMapping("/employees")
    public WxTableDataInfo list(EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();
        employeeVo.setOrgId(wxUser.getOrgIdLong());
        startPage();
        List<EmployeeVo> list = employeeService.selectEmployeeList(employeeVo);
        return getWxDataTable(list);
    }


    /**
     * 员工管理-机构新增修改员工  /institution/staff/add
     */
    @Log(title = "小程序机构新增修改员工", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('institution:staff:add')")
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@Validated @RequestBody EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();
        employeeVo.setOrgId(wxUser.getOrgIdLong());

        if (employeeVo.getId() == null) {
            AjaxResult res=employeeService.employeeUnifyAdd(employeeVo);
            res.put("data",res.get("msg"));
            return  res;
        } else {
            if (1L == employeeVo.getId()) {
                throw new ServiceException("不允许操作超级管理员用户");
            }
            AjaxResult res=employeeService.employeeUnifyEdit(employeeVo);
            res.put("data",res.get("msg"));
            return  res;
        }
    }


    /**
     * 员工管理-根据员工获取信息     /institution/staff  "/", "/{userId}"
     */
    @PostMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestBody EmployeeVo employeeVo) {
        Map res = new HashMap();
        WxUserDto wxUser = getWxUser();
        Long userId = employeeVo.getId();
        employeeVo.setOrgId(wxUser.getOrgIdLong());
        AjaxResult ajax =employeeService.employeeUnifyGetInfo(employeeVo);
        if(ajax.isError()){
            return ajax;
        }
        res.put("roles", ajax.get("roles"));
        if (StringUtils.isNotNull(userId)) {
            res.put("info", ajax.get("data"));
            res.put("roleIds", ajax.get("roleIds"));
        }
        ajax.put(AjaxResult.DATA_TAG, res);
        return ajax;
    }


    /* --------------------------  以下为老师管理接口  ------------------------------*/

    /**
     * 老师管理-全部老师    /institution/staff/teacherEmployees
     */
    @GetMapping("/teacherEmployees")
    public WxTableDataInfo teacherEmployees(EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();
        employeeVo.setOrgId(wxUser.getOrgIdLong());
        startPage();
        List<EmployeeVo> list = employeeService.selectTeacherEmployees(employeeVo);
        return getWxDataTable(list);
    }

    /**
     * 老师管理-基本信息 (小程序新建)
     */
    @PostMapping("/teacherInfo")
    public AjaxResult teacherInfo(@RequestBody EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();

        SysUser sysUser = sysUserService.getById(employeeVo.getId());
        EmployeeVo employee = new EmployeeVo();
        BeanUtil.copyProperties(sysUser, employee);
        QueryWrapper<SysOrgUser> qwou = new QueryWrapper<>();
        qwou.eq("org_id", wxUser.getOrgId());
        qwou.eq("user_id", sysUser.getId());
        SysOrgUser orgUser = sysOrgUserService.getOne(qwou);
        employee.setNickName(orgUser.getNickName());
        employee.setIsTeach(orgUser.getIsTeach());
        employee.setIsJob(orgUser.getIsJob());
        employee.setGenderName(sysDictDataService.getDictDataNameByType("sys_user_sex",sysUser.getGender()));
        return success(employee);
    }

    /**
     * 老师管理-当前授课班级 /institution/staff/teacherClass
     */
    @GetMapping("/teacherClass")
    public WxTableDataInfo teacherClass(EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();
        employeeVo.setOrgId(wxUser.getOrgIdLong());
        startPage();
        List<EmployeeInfoDto> teacherClassList = employeeService.getTeacherClassList(employeeVo);
        return getWxDataTable(teacherClassList);
    }

    /**
     * 老师管理-学员 (小程序新建)
     */
    @GetMapping("/teacherStudents")
    public WxTableDataInfo teacherStudents(EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();
        employeeVo.setOrgId(wxUser.getOrgIdLong());
        startPage();
        List<EmployeeStudentDto> teacherStudentList = employeeService.getTeacherStudentList(employeeVo);
        return getWxDataTable(teacherStudentList);
    }




//    /**
//     * 线索管理-跟进人
//     */
//    @GetMapping("/listStaff")
//    public WxTableDataInfo listStaff(EmployeeVo employeeVo) {
//        WxUserDto wxUser = getWxUser();
//        employeeVo.setOrgId(wxUser.getOrgIdLong());
//        startPage();
//        List<EmployeeVo> employeeVos = employeeService.selectUnifyListStaff(employeeVo);
//        return getWxDataTable(employeeVos);
//    }

    /* --------------------------  以下为线索学员接口  ------------------------------*/

    /**
     * 学员创建人列表 (包含学员人数)   /institution/staffStudent/followPeopleList
     * @return
     */
    @GetMapping("/followPeopleList")
    public WxTableDataInfo followPeopleList(EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();
        employeeVo.setOrgId(wxUser.getOrgIdLong());
        startPage();
        List<EmployeeVo> list =  employeeService.selectUnifyListStaff(employeeVo);
        return getWxDataTable(list);
    }
    /**
     * 学管师列表   /institution/staffStudent/managerList
     * @return
     */
    @GetMapping("/managerList")
    public AjaxResult managerList(EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();
        employeeVo.setOrgId(wxUser.getOrgIdLong());
        startPage();
        List<EmployeeVo> list = employeeService.managerList(employeeVo);
        return AjaxResult.success(list);
    }
    /**
     * 班级老师  /institution/staffStudent/teacherList
     * @return
     */
    @PostMapping("/teacherList")
    public AjaxResult teacherList(@RequestBody EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();
        employeeVo.setOrgId(wxUser.getOrgIdLong());
        List<EmployeeVo> list = employeeService.teacherList(employeeVo);
        return AjaxResult.success(list);
    }

    //-------------------------------------课次详情 ---------------------
    /***
     * 获取老师 助教老师下拉列表    /institution/staffStudent/peopleList
     */
    @PostMapping("/peopleList")
    public AjaxResult peopleList(@RequestBody EmployeeVo employeeVo) {
        WxUserDto wxUser = getWxUser();
        employeeVo.setOrgId(wxUser.getOrgIdLong());
        List<EmployeeVo> list = employeeService.selectEmployeeList(employeeVo);
        ArrayList<Object> list1 = new ArrayList<>();
        for (EmployeeVo sysUsers : list) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("label", sysUsers.getNickName());
            map.put("value", sysUsers.getId());
            list1.add(map);
        }
        return success(list1);
    }

}
